package com.yq.activemq.message_filter;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消息过滤demo
 */

public class Sender {

    public static final String BROKER_URL = "tcp://localhost:61616";

    public static void main(String[] args) {
        Sender sender = new Sender();
        sender.sendMessage("Hello ActiveMQ.");
    }

    private void sendMessage(String msg) {
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;

        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
            connection = connectionFactory.createConnection();
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

            Queue destination = session.createQueue("my-filter-queue");
            producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            for (int i = 0; i < 20; i++) {
                TextMessage textMessage = session.createTextMessage(msg + i);
                textMessage.setIntProperty("id", i);
                producer.send(textMessage);
            }

        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != producer) {
                    producer.close();
                }
                if (null != session) {
                    session.close();
                }
                if (null != connection) {
                    connection.close();
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
